<?php

function do_html_header($pagename = '') {
	// print an HTML header

	// declare the session variables we want access to inside the function
	if (!$_SESSION['items']) {
		$_SESSION['items'] = '0';
	}
	if (!$_SESSION['total_price']) {
		$_SESSION['total_price'] = '0.00';
	}
		// get categories out of database
		if (!$_SESSION['cat_array']) {
		$_SESSION['cat_array'] = get_categories();
	}

	?>
	<html>
	<head>
		<title><?php echo $pagename; ?></title>
	<link href="css-include/mystyle.css" type="text/css" rel="stylesheet" />
	</head>
	<body>
		<div id="wrapper">
		<div id="header">
		</div>
		<div id="body-filler">
			<div id="marquee">
				<ul>
					<li><img src="images/thumbs/thumb1.jpg" alt="" /></li>
					<li class="marleft5"><img src="images/thumbs/thumb2.jpg" alt="" /></li>
					<li class="marleft5"><img src="images/thumbs/thumb3.jpg" alt="" /></li>
				</ul>
			</div>
			<div id="mainbody">
<?php
	if($title) {
		do_html_heading($title);
	}
}

function do_html_footer() {
	// print an HTML footer
?>
			</div>
		</div>
	</div>
	<div id="body-filler-bottom"></div>
	</div>
	</body>
	</html>
<?php
}

function do_html_heading($heading) {
	// print heading
?>
	<h2><?php echo $heading; ?></h2>
<?php
}

function do_html_URL($url, $name) {
	// output URL as link and br
?>
	<a href="<?php echo $url; ?>"><?php echo $name; ?></a><br />
<?php
}

function display_shopping_menu($cat_array) {
	?>
	<div id="left-area">
		<ul id="left-navigation">
		<p>Please choose a category:</p>
			<?php
			if (!is_array($cat_array)) {
			 echo "<p>No categories currently available</p>";
			 return;
			}
			//echo "<ul>";
			foreach ($cat_array as $row)	{
			$url = "show_cat.php?catid=".$row['catid'];
			$clipname = $row['catname'];
			echo "<li>";
			do_html_url($url, $clipname);
			echo "</li>";
			}
			echo "</ul>";
			echo "<hr />";
			if (check_admin_user()) {
				do_html_url("admin.php", "Admin Menu");
			}
			echo "</div>\n";
}

function display_paperclips($paperclip_array) {
?>
<div id="right-area">
<?php
	//display all paper clips in the array passed in
	if (!is_array($paperclip_array)) {
		echo "<p>No paper clips currently available in this category</p>";
	} else {
		//create table
		echo "<table width=\"620\" cellpadding=\"0\" cellspacing=\"0\" style=\"text-align:center; vertical-align:middle\">";

		//create a table cell for each paper clip
		for ( $i = 0; $i < count( $paperclip_array ); $i++ )
		{
			$row = $paperclip_array[$i];
			$url = "show_paperclip.php?sku=".$row['sku'];
			if ( $i % 2 == 0 ){
			echo "<tr>";
			}
			echo "<td><ul>";
			if (@file_exists("images/paperclips/".$row['sku'].".jpg")) {
				$clipname = "<img src=\"images/paperclips/".$row['sku'].".jpg\"
									style=\"border: 1px solid black\"/>";
				echo "<li>";
				do_html_url($url, $clipname);
				echo "</li>";
			} 
			$clipname = $row['clipname'];
			echo "<li>";
			do_html_url($url, $clipname);
			echo "</li>";
			echo "<li>";
			display_button("show_cart.php?new=".$row['sku'], "add-to-cart",
									 "Add".$paperclip['title']." To My Shopping Cart");
			echo "<li>";
			echo "</td>";
			if ( $i % 2 == 1 ){
				echo "</tr>";
			}
		}
		if ( count( $paperclip_array ) % 2 == 1 )
		{
			echo "</tr>";
		}
		echo "</table>";
	}

	echo "<hr />";
}

function display_paperclip_details($paperclip) {
?>
<div id="right-area">
<?php
	// display all details about this paper clip
	if (is_array($paperclip)) {
		echo "<table><tr>";
		//display the picture if there is one
		if (@file_exists("images/paperclips/".$paperclip['sku'].".jpg"))	{
			$size = GetImageSize("images/paperclips/".$paperclip['sku'].".jpg");
			if(($size[0] > 0) && ($size[1] > 0)) {
				echo "<td><img src=\"images/paperclips/".$paperclip['sku'].".jpg\"
							style=\"border: 1px solid black\"/></td>";
			}
		}
		echo "<td><ul>";
		echo "<li><strong>Designer:</strong> ";
		echo $paperclip['designer'];
		echo "</li><li><strong>SKU:</strong> ";
		echo $paperclip['sku'];
		echo "</li><li><strong>Our Price:</strong> ";
		echo number_format($paperclip['price'], 2);
		echo "</li><li><strong>Description:</strong> ";
		echo $paperclip['description'];
		echo "</li></ul></td></tr></table>";
	} else {
		echo "<p>The details of this paper clip cannot be displayed at this time.</p>";
	}
	echo "<hr />";
}

function display_checkout_form() {
	//display the form that asks for name and address
?>
	<br />
	<table border="0" width="100%" cellspacing="0" bgcolor="#cccccc">
	<form action="purchase.php" method="post">
	<tr><th colspan="2" bgcolor="#cccccc">Your Details</th></tr>
	<tr bgcolor="#cccccc">
		<td>Name</td>
		<td><input type="text" name="name" value="" maxlength="40" size="40"/></td>
	</tr>
	<tr bgcolor="#cccccc">
		<td>Address</td>
		<td><input type="text" name="address" value="" maxlength="40" size="40"/></td>
	</tr>
	<tr bgcolor="#cccccc">
		<td>City/Suburb</td>
		<td><input type="text" name="city" value="" maxlength="20" size="40"/></td>
	</tr>
	<tr bgcolor="#cccccc">
		<td>State/Province</td>
		<td><input type="text" name="state" value="" maxlength="20" size="40"/></td>
	</tr>
	<tr bgcolor="#cccccc">
		<td>Postal Code or Zip Code</td>
		<td><input type="text" name="zip" value="" maxlength="10" size="40"/></td>
	</tr>
	<tr bgcolor="#cccccc">
		<td>Country</td>
		<td><input type="text" name="country" value="" maxlength="20" size="40"/></td>
	</tr>
	<tr><th colspan="2" bgcolor="#cccccc">Shipping Address (leave blank if as above)</th></tr>
	<tr bgcolor="#cccccc">
		<td>Name</td>
		<td><input type="text" name="ship_name" value="" maxlength="40" size="40"/></td>
	</tr>
	<tr bgcolor="#cccccc">
		<td>Address</td>
		<td><input type="text" name="ship_address" value="" maxlength="40" size="40"/></td>
	</tr>
	<tr bgcolor="#cccccc">
		<td>City/Suburb</td>
		<td><input type="text" name="ship_city" value="" maxlength="20" size="40"/></td>
	</tr>
	<tr bgcolor="#cccccc">
		<td>State/Province</td>
		<td><input type="text" name="ship_state" value="" maxlength="20" size="40"/></td>
	</tr>
	<tr bgcolor="#cccccc">
		<td>Postal Code or Zip Code</td>
		<td><input type="text" name="ship_zip" value="" maxlength="10" size="40"/></td>
	</tr>
	<tr bgcolor="#cccccc">
		<td>Country</td>
		<td><input type="text" name="ship_country" value="" maxlength="20" size="40"/></td>
	</tr>
	<tr>
		<td colspan="2" align="center"><p><strong>Please press Purchase to confirm
				 your purchase, or Continue Shopping to add or remove items.</strong></p>
		 <?php display_form_button("purchase", "Purchase These Items"); ?>
		</td>
	</tr>
	</form>
	</table><hr />
<?php
}

function display_shipping($shipping) {
	// display table row with shipping cost and total price including shipping
?>
	<table border="0" width="100%" cellspacing="0">
	<tr><td align="left">Shipping</td>
			<td align="right"> <?php echo number_format($shipping, 2); ?></td></tr>
	<tr><th bgcolor="#cccccc" align="left">TOTAL INCLUDING SHIPPING</th>
			<th bgcolor="#cccccc" align="right">$ <?php echo number_format($shipping+$_SESSION['total_price'], 2); ?></th>
	</tr>
	</table><br />
<?php
}

function display_card_form($name) {
	//display form asking for credit card details
?>
	<table border="0" width="100%" cellspacing="0">
	<form action="process.php" method="post">
	<tr><th colspan="2" bgcolor="#cccccc">Credit Card Details</th></tr>
	<tr>
		<td>Type</td>
		<td><select name="card_type">
				<option value="VISA">VISA</option>
				<option value="MasterCard">MasterCard</option>
				<option value="American Express">American Express</option>
				</select>
		</td>
	</tr>
	<tr>
		<td>Number</td>
		<td><input type="text" name="card_number" value="" maxlength="16" size="40"></td>
	</tr>
	<tr>
		<td>AMEX code (if required)</td>
		<td><input type="text" name="amex_code" value="" maxlength="4" size="4"></td>
	</tr>
	<tr>
		<td>Expiry Date</td>
		<td>Month
			 <select name="card_month">
			 <option value="01">01</option>
			 <option value="02">02</option>
			 <option value="03">03</option>
			 <option value="04">04</option>
			 <option value="05">05</option>
			 <option value="06">06</option>
			 <option value="07">07</option>
			 <option value="08">08</option>
			 <option value="09">09</option>
			 <option value="10">10</option>
			 <option value="11">11</option>
			 <option value="12">12</option>
			 </select>
			 Year
			 <select name="card_year">
			 <?php
			 for ($y = date("Y"); $y < date("Y") + 10; $y++) {
				 echo "<option value=\"".$y."\">".$y."</option>";
			 }
			 ?>
			 </select>
	</tr>
	<tr>
		<td>Name on Card</td>
		<td><input type="text" name="card_name" value = "<?php echo $name; ?>" maxlength="40" size="40"></td>
	</tr>
	<tr>
		<td colspan="2" align="center">
			<p><strong>Please press Purchase to confirm your purchase, or Continue Shopping to
			add or remove items</strong></p>
		 <?php display_form_button('purchase', 'Purchase These Items'); ?>
		</td>
	</tr>
	</table>
<?php
}

function display_cart($cart, $change = true, $images = 1) {
	// display items in shopping cart
	// optionally allow changes (true or false)
	// optionally include images (1 - yes, 0 - no)

	 echo "<table border=\"0\" width=\"100%\" cellspacing=\"0\">
				 <form action=\"show_cart.php\" method=\"post\">
				 <tr><th colspan=\"".(1 + $images)."\" bgcolor=\"#cccccc\">Item</th>
				 <th bgcolor=\"#cccccc\">Price</th>
				 <th bgcolor=\"#cccccc\">Quantity</th>
				 <th bgcolor=\"#cccccc\">Total</th>
				 </tr>";

	//display each item as a table row
	foreach ($cart as $sku => $qty)	{
		$paperclip = get_paperclip_details($sku);
		echo "<tr bgcolor=\"#cccccc\">";
		if($images == true) {
			echo "<td align=\"left\">";
			if (file_exists("images/".$sku.".jpg")) {
				 $size = GetImageSize("images/".$sku.".jpg");
				 if(($size[0] > 0) && ($size[1] > 0)) {
					 echo "<img src=\"images/".$sku.".jpg\"
									style=\"border: 1px solid black\"
									width=\"".($size[0]/3)."\"
									height=\"".($size[1]/3)."\"/>";
				 }
			} else {
				 echo "&nbsp;";
			}
			echo "</td>";
		}
		echo "<td align=\"left\">
					<a href=\"show_paperclip.php?sku=".$sku."\">".$paperclip['clipname']."</a>
					<td align=\"center\">\$".number_format($paperclip['price'], 2)."</td>
					<td align=\"center\">";

		// if we allow changes, quantities are in text boxes
		if ($change == true) {
			echo "<input type=\"text\" name=\"".$sku."\" value=\"".$qty."\" size=\"3\">";
		} else {
			echo $qty;
		}
		echo "</td><td align=\"center\">\$".number_format($paperclip['price']*$qty,2)."</td></tr>\n";
	}
	// display total row
	echo "<tr>
				<th colspan=\"".(2+$images)."\" bgcolor=\"#cccccc\">&nbsp;</td>
				<th align=\"center\" bgcolor=\"#cccccc\">".$_SESSION['items']."</th>
				<th align=\"center\" bgcolor=\"#cccccc\">
						\$".number_format($_SESSION['total_price'], 2)."
				</th>
				</tr>";

	// display save change button
	if($change == true) {
		echo "<tr>
					<td colspan=\"".(2+$images)."\">&nbsp;</td>
					<td align=\"center\">
						 <input type=\"hidden\" name=\"save\" value=\"true\"/>
						 <input type=\"image\" src=\"images/save-changes.png\"
										border=\"0\" alt=\"Save Changes\"/>
					</td>
					<td>&nbsp;</td>
					</tr>";
	}
	echo "</form></table>";
}

function display_login_form() {
	// dispaly form asking for name and password
?>
 <h1>Admin Login</h1>
 <form method="post" action="admin.php">
 <table bgcolor="#cccccc" align="center">
	 <tr>
		 <td>Username:</td>
		 <td><input type="text" name="username"/></td></tr>
	 <tr>
		 <td>Password:</td>
		 <td><input type="password" name="passwd"/></td></tr>
	 <tr>
		 <td colspan="2" align="center">
		 <input type="submit" value="Log in"/></td></tr>
	 <tr>
 </table></form>
<?php
}

function display_admin_menu() {
?>
	<div id="left-area">
		<ul id="left-navigation">
			<li><a href="index.php">Go to main site</a></li>
			<li><a href="insert_category_form.php">Add a new category</a></li>
			<li><a href="insert_paperclip_form.php">Add a new paper clip</a></li>
			<li><a href="change_password_form.php">Change admin password</a></li>
			<li><a href="logout.php">Log Out</a></li>
		</ul>
	 </div>
<?php
}

function display_button($target, $image, $alt) {
	echo "<div><a href=\"".$target."\">
					<img src=\"images/".$image.".png\"
					 alt=\"".$alt."\" border=\"0\" height=\"51\"
					 width=\"135\"/></a></div>";
}

function display_form_button($image, $alt) {
	echo "<div align=\"center\"><input type=\"image\"
					 src=\"images/".$image.".png\"
					 alt=\"".$alt."\" border=\"0\" height=\"50\"
					 width=\"135\"/></div>";
}

function display_cart_tool() {
?>
<div id="shopping-cart">
	<table width="100%" border="0" cellspacing="0">
	<tr><td width="485px">
	<?php
	if(isset($_SESSION['admin_user'])) {
		echo "&nbsp;";
	} else {
		echo "<p align=\"right\">Total Items = ".$_SESSION['items']."&nbsp;<br \>";
		echo "Total Price = $".number_format($_SESSION['total_price'],2)."&nbsp;</p>";
	}
	?>
	</td><td>
	<?php
	if(isset($_SESSION['admin_user'])) {
		display_button('logout.php', 'log-out', 'Log Out');
	} else {
		display_button('show_cart.php', 'view-cart', 'View Your Shopping Cart');
	}

	?>
	</td></td></table>
</div>

<?php
}



?>
